﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using shfa.snomtastic.data;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eUsers : EditBase
    {
    
        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
             BindGridItems();
            
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);
        }

        #endregion


        #region Event Handlers

        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                e.Canceled = true;
                Guid id = new Guid(rgListItems.Items[e.Item.ItemIndex].GetDataKeyValue("UserId").ToString());
                RaiseNavEvent(this, new NavEventArgs("eUser", id));
            }
        }

        #endregion

        public void BindGridItems()
        {
            UserController uc = new UserController();
            List<UserInfo> uiList = uc.SelectUsers();
            foreach (UserInfo ui in uiList)
            {
                ui.PasswordUpdate = ui.PasswordUpdate.ToLocalTime();
                ui.PasswordUpdateAD = ui.PasswordUpdateAD.ToLocalTime();
                ui.LastAccessDate = ui.LastAccessDate.ToLocalTime();
            }

            rgListItems.DataSource = uiList;
            rgListItems.DataBind();
        }


        #region Action Events
        public void DeleteItem(Guid id)
        {
            UserController uc = new UserController();
            uc.DeleteUser(id);
            RaiseNavEvent(this, new NavEventArgs("eUsers", id));
            
        }


        #endregion


        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Refresh":
                    RaiseNavEvent(this, new NavEventArgs("eUsers", new Guid()));
                    break;
                case "Add":
                    RaiseNavEvent(this, new NavEventArgs("eUser", new Guid()));                        
                    break;
                case "Edit":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eUser", new Guid(rgListItems.SelectedValue.ToString())));
                    }
                    break;
                case "Delete":
                    if (rgListItems.SelectedValue != null)
                    {
                        DeleteItem(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                default:
                    break;
            }

          
        }

        public override List<ActionInfo> ActionList
        {
            get 
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Refresh", "Refresh Users", "~/images/icons/refresh.png"));
                al.Add(new ActionInfo("Add", "Add User", "~/images/icons/add.png"));
                al.Add(new ActionInfo("Edit", "Edit User", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("Delete", "Delete User", "~/images/icons/delete.png", "Are you sure you want to delete this user?", true));
                return al;
            }
        }

        #endregion

    }
}